{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IV- Demo. plot results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An example of data evaluation metrics have been produced in the \"demo_data_eval.ipynb\" notebook. Here, we illustrate the results. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import hvplot.xarray\n",
    "import numpy\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.mod_plot import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the reference SSH field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_ssh_reference = '../results/dc_ref_2012-10-22-2012-12-02_sample.nc'\n",
    "dc_ref_sample = xr.open_dataset(filename_ssh_reference)\n",
    "dc_ref_sample"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the reconstructed SSH field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_ssh_reconstruction_regridded = '../results/ssh_reconstruction_regridded_2012-10-22-2012-12-02_jason1.nc'\n",
    "dc_reconstruction_regridded = xr.open_dataset(filename_ssh_reconstruction_regridded)\n",
    "dc_reconstruction_regridded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_ssh_reconstruction = '../results/ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n",
    "dc_reconstruction = xr.open_dataset(filename_ssh_reconstruction)\n",
    "dc_reconstruction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display the reference & reconstructed SSH fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_concat = xr.concat((dc_ref_sample, dc_reconstruction_regridded), dim='field')\n",
    "ds_concat['field'] = [\"Nature Run\", \"OI Reconstruction\"]\n",
    "ds_concat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_concat.hvplot.image(x='lon', y='lat', cmap='gist_stern', by='field', width=500, height=400, widget_location='bottom', clabel='SSH [m]', subplots=True, clim=(-0.5, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display the difference  SSH_reconstruction minus SSH_reference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(dc_reconstruction_regridded - dc_ref_sample).hvplot.image(x='lon', y='lat', cmap='coolwarm', width=500, height=400, widget_location='bottom', clabel='dSSH[m]')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display spatially averaged daily RMSE score (normalized RMSE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Shows the temporal stability of the reconstuction. A score of 1 means a perfect reconstruction, a zero score means strong incoherency between the reconstruction & reference fields "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_rmse_t = '../results/rmse_t_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n",
    "ds = xr.open_dataset(filename_rmse_t)\n",
    "ds.rmse_t.hvplot(yaxis='left', title='Spatially averaged daily RMSE', ylim=(0, 1), ylabel='RMSE score')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dc_reconstruction.nobs.hvplot.area(color='grey', title='# obs in OI')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display temporally averaged RMSE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Highlight the regions of large/low mean reconstruction errors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_rmse_xy = '../results/rmse_xy_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n",
    "ds = xr.open_dataset(filename_rmse_xy)\n",
    "ds.rmse_xy.hvplot(x='lon', y='lat', cmap='Reds', width=500, height=400, clabel='RMSE[m]', title='Temporally averaged RMSE')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display PSD score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Highlight the average space-time scales that are resolved in the reconstruction. The boundary is delimited by the iso-contour 0.5 (green area in the figure)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_psd = '../results/psd_ssh_reconstruction_2012-10-22-2012-12-02_jason1.nc'\n",
    "ds = xr.open_dataset(filename_psd)\n",
    "plot_psd_score(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds['freq_time'] = 1./ds['freq_time']\n",
    "ds['freq_lon'] = 1./ds['freq_lon']\n",
    "ds.hvplot.quadmesh(x='freq_lon', y='freq_time', z='psd_score', cmap='RdYlGn',\n",
    "                   clim=(0, 1), logx=False, logy=False, title='PSD-score', width=500, height=400, \n",
    "                   clabel='PSD score', xlabel='wavelenght (degree)', ylabel='wavelenght (days)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
